package com.adlotto2.lcdns.activity.info;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.cronos.http.CNLottoAdApiSet;
import com.adlotto2.lcdns.util.ApplicationUtils;
import com.adlotto2.lcdns.util.LottoAdOverButton;

/**
 * 관심분야 선택 Activity
 * @author Administrator
 *
 */
public class LottoAdInfoFavoriteActivity extends Activity{
	
	private final int REQUEST_ACTIVITY_RECOMMENDER = 9999;
	
	/** 선택한관심분야 String 배열 **/
	private String[] mFavoriteArr = new String[5];
	/** 선택한관심분야 **/
	private String mFavorite;

	private Context mContext;
	
	/** 다음(완료) 버튼 **/
	private Button mNextBtn;
	/** 전체선택 버튼 **/
	private Button mAllCheckBtn;
	
	/** 각 관심분야 체크박스 **/
	private CheckBox mLeserChk;
	private CheckBox mCultureChk;
	private CheckBox mHealthChk;
	private CheckBox mCashChk;
	private CheckBox mShoppingChk;
	
	private ArrayList<Boolean> mIsCheckArr = new ArrayList<Boolean>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lottoad_info_favorite);
		
		this.mContext = this;
		
		mLeserChk = (CheckBox) findViewById(R.id.checkbox_favorite_leser);
		mCultureChk = (CheckBox) findViewById(R.id.checkbox_favorite_culture);
		mHealthChk = (CheckBox) findViewById(R.id.checkbox_favorite_health);
		mCashChk = (CheckBox) findViewById(R.id.checkbox_favorite_cash);
		mShoppingChk = (CheckBox) findViewById(R.id.checkbox_favorite_shopping);
		
		mNextBtn = (Button) findViewById(R.id.button_next);
		mAllCheckBtn = (Button) findViewById(R.id.button_check_all);
		
		mLeserChk.setOnCheckedChangeListener(checkedListener);
		mCultureChk.setOnCheckedChangeListener(checkedListener);
		mHealthChk.setOnCheckedChangeListener(checkedListener);
		mCashChk.setOnCheckedChangeListener(checkedListener);
		mShoppingChk.setOnCheckedChangeListener(checkedListener);
		
		// 다음버튼 누를시 finish 한후 activityForResult로 intent(data)를 넘긴다.
		mNextBtn.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					if (areChecked()) {
						/*####### Ben ########*/
						// 회원가입 한단계 추가로 인함.
						/*Intent data = new Intent();
						data.putExtra("favorite", sendFavorite());
						setResult(RESULT_OK,data);
						finish();*/
						Intent intent = new Intent(mContext, LottoAdInfoRecommenderActivity.class);
						startActivityForResult(intent, REQUEST_ACTIVITY_RECOMMENDER);
						
					} else {
						ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), getString(R.string.favorite_doNotSelected));
					}
				}
				return false;
			}
		});
		
		mAllCheckBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					
					// 관심분야 체크박스 모두 체크
					mLeserChk.setChecked(true);
					mCultureChk.setChecked(true);
					mHealthChk.setChecked(true);
					mCashChk.setChecked(true);
					mShoppingChk.setChecked(true);

					// 관심분야 String배열에 값 할당					
					mFavoriteArr[0] = "l";
					mFavoriteArr[1] = "c";
					mFavoriteArr[2] = "h";
					mFavoriteArr[3] = "m";
					mFavoriteArr[4] = "s";
				}
				return false;
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_ACTIVITY_RECOMMENDER && resultCode == RESULT_OK) {
			
//			extras.putString("favorite", sendFavorite());
//			extras.putStringArrayLis("recommenderId", data.getStringExtra("recommenderId"));
			String favorite = sendFavorite();
			
			
			
			data.putExtra("favorite", favorite);
			
			// 추천인 id는 sharedPreference로 전달함.
			ApplicationUtils.setStrPreferences(this, "recommenderId", data.getStringExtra("recommenderId"));
			setResult(RESULT_OK, data);
			finish();
			if(!CNLottoAdApiSet.isLive()) {
				Log.i("BenTag", "Activity result!!!! recommender id : " + data.getStringExtra("recommenderId") + ", favorite : " + favorite);
			}
		}
	}
	
	private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			
			if(isChecked){ // 체크 할시
				
				// 체크 카운트 배열에 true를 추가 한다
				mIsCheckArr.add(true);
				
				// 각 해당 체크박스 선택시 해당 관심분야 String 배열에 값 할당
				switch (buttonView.getId()) {
				case R.id.checkbox_favorite_leser:
					mFavoriteArr[0] = "l";
					break;
				case R.id.checkbox_favorite_culture:
					mFavoriteArr[1] = "c";
					break;
				case R.id.checkbox_favorite_health:
					mFavoriteArr[2] = "h";
					break;
				case R.id.checkbox_favorite_cash:
					mFavoriteArr[3] = "m";
					break;
				case R.id.checkbox_favorite_shopping:
					mFavoriteArr[4] = "s";
					break;
				}
			}
			else{ // 체크 해제 할시
				
				// 체크 카운트 배열에 true를 제거 한다				
				mIsCheckArr.remove(true);
				
				// 각 해당 체크박스 해제시 해당 관심분야 String 배열에 값 초기화
				switch (buttonView.getId()) {
				case R.id.checkbox_favorite_leser:
					mFavoriteArr[0]="";
					break;
				case R.id.checkbox_favorite_culture:
					mFavoriteArr[1]="";
					break;
				case R.id.checkbox_favorite_health:
					mFavoriteArr[2]="";
					break;
				case R.id.checkbox_favorite_cash:
					mFavoriteArr[3]="";
					break;
				case R.id.checkbox_favorite_shopping:
					mFavoriteArr[4]="";
					break;
				}
			}
			
		}
	}; 
	
	/** 전송할 관심분야 목록 **/
	private String sendFavorite(){
		for(int i=0; i<5; i++){
			if(mFavoriteArr[i] != null && !mFavoriteArr[i].equals("")){
				if(mFavorite == null){
					mFavorite="";
				}
				mFavorite+= (mFavoriteArr[i] + ",");
			}
		}
		
		return mFavorite;
	}
	
	/** 관심분야 체크 카운트 **/
	private boolean areChecked(){
		if(mIsCheckArr.size() < 3){
			return false;
		} else {
			return true;
		}
	}
}
